#include "Thread.hpp"
#include "block.hpp"
#include "Task.hpp"
#include <list>
int a = 10;

using block_t=BlockQueue<Task>;
// void print()
// {
//     std::cout<<"hello"<<a+b<<std::endl;
// }
void Consumer(block_t&bq)
{
    while (1)
    {
     // 1. 从blockqueue取下来任务
        Task t;
        bq.Pop(&t);
        // 2. 处理这个任务
       
        //t.Excute(); //消费者私有
         std::cout << "Consumer Consum data is : " << t.ResultToString() << std::endl;
    }
}
void Producter(block_t&bq)
{
    srand(time(nullptr)^pthread_self());
    int cnt = 10;
    while (true)
    {
        sleep(1);
        // 1. 获取任务
        int a = rand() % 10 + 1;
        usleep(1234);
        int b = rand() % 20 + 1;
        Task t(a, b);
        // 2. 把获取的任务，放入blockqueue
        //Task t = print;
        bq.Enqueue(t);
         std::cout << "Productor product data is : " << t.DebugToString() << std::endl;
    }
}
//模板记得删 不然乱报错
void StartComm(std::vector<Thread<block_t>> *threads, int num,block_t &bq, func_t<block_t> func)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads->emplace_back(func, bq, name);
        threads->back().Start();
    }
}
void StartConsumer(std::vector<Thread<block_t>> *threads, int num, block_t&bq)
{
    StartComm(threads, num, bq, Consumer);
}
void startProductor(std::vector<Thread<block_t>> *threads, int num,block_t&bq)
{
     StartComm(threads, num, bq, Producter);
}
void WaitAllThreads(std::vector<Thread<block_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}
int main()
{
    block_t *bq = new block_t(6);
    std::vector<Thread<block_t>> threads;
    StartConsumer(&threads, 3, *bq);/////////////////////////////////////////
    startProductor(&threads, 2, *bq);
    WaitAllThreads(threads);
    return 0;
}